Abraxus's Blog

RaRCTF verybabyrev Write Up

Details:

Jeopardy style CTF

Category: Reverse Engineering

Points: 100

Comments: fun fact: verybabyrev backwards is verybabyrev

Write up:

Opening up the main function in a decompiler I saw this:

int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
  __int64 s1[12];
  char v4;
  char s[140];
  int v6; 

  setvbuf(stdout, 0LL, 2, 0LL);
  memset(s, 0, 0x80uLL);
  s1[0] = 0x45481D1217111313LL;
  s1[1] = 0x95F422C260B4145LL;
  s1[2] = 0x541B56563D6C5F0BLL;
  s1[3] = 0x585C0B3C2945415FLL;
  s1[4] = 0x402A6C54095D5F00LL;
  s1[5] = 0x4B5F4248276A0606LL;
  s1[6] = 0x6C5E5D432C2D4256LL;
  s1[7] = 0x6B315E434707412DLL;
  s1[8] = 0x5E54491C6E3B0A5ALL;
  s1[9] = 0x2828475E05342B1ALL;
  s1[10] = 0x60450073B26111FLL;
  s1[11] = 0xA774803050B0D04LL;
  v4 = 0;
  printf("Enter your flag: ");
  fgets(s, 128, stdin);
  v6 = 0;
  if ( s[0] != 114 )
  {
    puts("Nope!");
    exit(0);
  }
  while ( v6 <= 126 )
  {
    s[v6] ^= s[v6 + 1];
    ++v6;
  }
  if ( !memcmp(s1, s, 0x61uLL) )
  {
    puts("Correct!");
    exit(1);
  }
  puts("Nope!");
  exit(0);
}

The first thing I did was extract s1 since that was going to be the encrypted flag:

[0x13, 0x13, 0x11, 0x17, 0x12, 0x1D, 0x48, 0x45, 0x45, 0x41, 0x0B, 0x26, 0x2C, 0x42, 0x5F, 0x09, 0x0B, 0x5F, 0x6C, 0x3D, 0x56, 0x56, 0x1B, 0x54, 0x5F, 0x41, 0x45, 0x29, 0x3C, 0x0B, 0x5C, 0x58, 0x00, 0x5F, 0x5D, 0x09, 0x54, 0x6C, 0x2A, 0x40, 0x06, 0x06, 0x6A, 0x27, 0x48, 0x42, 0x5F, 0x4B, 0x56, 0x42, 0x2D, 0x2C, 0x43, 0x5D, 0x5E, 0x6C, 0x2D, 0x41, 0x07, 0x47, 0x43, 0x5E, 0x31, 0x6B, 0x5A, 0x0A, 0x3B, 0x6E, 0x1C, 0x49, 0x54, 0x5E, 0x1A, 0x2B, 0x34, 0x05, 0x5E, 0x47, 0x28, 0x28, 0x1F, 0x11, 0x26, 0x3B, 0x07, 0x50, 0x04, 0x06, 0x04, 0x0D, 0x0B, 0x05, 0x03, 0x48, 0x77, 0x0A]

The "encryption" was simply using the current character and xor'ing it with the next one. I knew the start of the flag would be rarctf{ so I wrote the following script:

# encrypted flag
enc = [0x13, 0x13, 0x11, 0x17, 0x12, 0x1D, 0x48, 0x45, 0x45, 0x41, 0x0B, 0x26, 0x2C, 0x42, 0x5F, 0x09, 0x0B, 0x5F, 0x6C, 0x3D, 0x56, 0x56, 0x1B, 0x54, 0x5F, 0x41, 0x45, 0x29, 0x3C, 0x0B, 0x5C, 0x58, 0x00, 0x5F, 0x5D, 0x09, 0x54, 0x6C, 0x2A, 0x40, 0x06, 0x06, 0x6A, 0x27, 0x48, 0x42, 0x5F, 0x4B, 0x56, 0x42, 0x2D, 0x2C, 0x43, 0x5D, 0x5E, 0x6C, 0x2D, 0x41, 0x07, 0x47, 0x43, 0x5E, 0x31, 0x6B, 0x5A, 0x0A, 0x3B, 0x6E, 0x1C, 0x49, 0x54, 0x5E, 0x1A, 0x2B, 0x34, 0x05, 0x5E, 0x47, 0x28, 0x28, 0x1F, 0x11, 0x26, 0x3B, 0x07, 0x50, 0x04, 0x06, 0x04, 0x0D, 0x0B, 0x05, 0x03, 0x48, 0x77, 0x0A]

# input flag, with known parts
s = "rarctf{"

# loop through the length of the encrypted bytes from length of known
for i in range(len(s)-1, len(enc) - 1):

    # try the range of characters
	for j in range(32, 127):

        # if the character gives us the encrypted bytes 
		if ord(s[i]) ^ j == enc[i]:

            # add the byte to the string and break the loop
			s += chr(j)
			break

# print the flag
print(s)

When run this printed out:

rarctf{3v3ry_s1ngl3_b4by-r3v_ch4ll3ng3_u535_x0r-f0r_s0m3_r34s0n_4nd_1-d0nt_kn0w_why_dc37158365}